<?php
// +----------------------------------------------------------------------
// | LwwanPHP
// +----------------------------------------------------------------------
// | 版权所有 似水星辰 [ 2630481389@qq.com ]
// +----------------------------------------------------------------------
// | 星辰工作室 http://www.sitejs.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------

namespace app\goods\validate;

use think\Validate;

/**
 * 商品编辑验证器
 * @package app\goods\validate
 * @author 晓风<215628355@qq.com>
 * @date 2021年8月13日16:06:56 加强版
 */
class Goods extends Validate
{
    //定义规则
    protected $rule = [
        'name|商品名称'    => [ 'require',"max"=>100],
        'cid|商品分类'     => [ 'require',"integer",'gt'=>0],
        'adslogan|广告语'  => ["max"=>100],        
        'sn|商品货号'      => ["max"=>60,"alphaDash"], //"unique"=>"goods",
        'keywords|关键字'  => ["max"=>20],             
        'brand_id|品牌'  => ["integer","egt"=>0],           
        
        'shop_price|本店价格'=> [ 'require','float',"gt"=>0,"lt"=>100000],
        'cost_price|成本价'  => ["float","elt"=>'shop_price'],     
        'market_price|划线价'=> ["float","egt"=>'shop_price'],           
    
        'stock|总库存'      => ['requireIf'=>'is_spec,0',"integer","egt"=>0,'gtIf'=>'is_spec,0,0'], 
        "weight|重量"       => ['requireIf'=>'is_spec,0',"integer","egt"=>0,'gtIf'=>'is_spec,0,0'], 

        'thumb|缩略图'     => ['require',"integer","gt"=>0], 
        'video|视频'     => ["integer","egt"=>0], 
        'images|图册'      => ['require',"checkIds"=>9],      
        'description|商品简介'    => ["max"=>200],
	'body|商品详情'    => [ 'require',"max"=>65535],
        
        'is_spec|规格'     => ['require',"in"=>[0,1]],    
        'item|规格值'     => ['requireIf'=>'is_spec,1',"checkItems"],  
        
        //'give_integral|自购返积分'  => ["integer","egt"=>0,"lt"=>'shop_price'], 
        //'commission|带货佣金'  => ["float","egt"=>0,"lt"=>'shop_price'], 
        
        'is_shipping|是否包邮'     => ['require',"in"=>[0,1]],         
        'freight_template_id|运费模板'  => ['require',"integer","gt"=>0,],  //requireIf:account,1
        'amount_condition|满金额包邮条件'=> ["float","egt"=>0], 
        
        'sales_sum|销量'    =>  ["integer","egt"=>0], 
    ];

    protected $message = [
        'cost_price.elt'  => '成本价不可高于售价',
        'market_price.egt'     => '划线价不可低于售价',
       // 'give_integral.lt'     => '返积分不可超过售价',
       // 'commission.lt'     => '带货佣金不可超过售价',
        "weight.requireIf"=>"重量必须填写",
        "item.requireIf"=>"规格必须填写",
        "freight_template_id.requireIf"=>"运费模板必须设置",
        "freight_template_id.gtIf"=>"运费模板必须设置",
    ];

    // 定义验证场景
    protected $scene = [
      
    ];
    
    

    private $itemRole = [        
        
        'key_name|规格名称'    => [ "max"=>100],
        'sku_sn|货号'     => ["max"=>60,"alphaDash"],
        
        'shop_price|本店价格'=> [ 'require','float',"gt"=>0,"lt"=>100000],
        'cost_price|成本价'  => ["float","elt"=>'shop_price'],     
        'market_price|划线价'=> ["float","egt"=>'shop_price'],   
        
        'stock|库存'      => ['require',"integer","gt"=>0], 
        "sku_weight|重量"     => ['require',"integer","gt"=>0], 
        
        //'give_integral|自购返积分'  => ["integer","egt"=>0,"lt"=>'shop_price'], 
        //'commission|带货佣金'  => ["float","egt"=>0,"lt"=>'shop_price'], 
        
        'status|状态'     =>  ["in"=>[0,1]], 
        'sku_id|规格ID'   =>  ["integer","gt"=>0], 

    ];
    
    private $itemMessage = [
        'cost_price.elt'  => '成本价不可高于售价',
        'market_price.egt'     => '划线价不可低于售价',
        
        //'give_integral.lt'     => '返积分不可超过售价',
        //'commission.lt'     => '带货佣金不可超过售价',      
    ];


    /**
     * Ids验证器
     * @author 晓风<215628355@qq.com>
     * @param string $value 值
     * @param int $rule 规则
     * @param array $data 数据
     * @param string $filed 字段
     * @return boolean
     */
    protected function checkIds($value,$rule,$data=[],$filed = ""){
      
        $arr = explode(",",$value);
        if(count($arr) > $rule){
            return  ":attribute最多可选". $rule. "个";
        }
        foreach($arr as $v){
            if(!preg_match( '/^\d+$/',$v)){
                return ":attribute只能是数字列表组成";
            }
        } 
        return true;
    }
    
     /**
     * 某字段等于值大于0
     * @author 晓风<215628355@qq.com>
     * @param string $value 值
     * @param int $rule 规则
     * @param array $data 数据
     * @param string $filed 字段
     * @return boolean
     */
    protected function gtIf($value,$rule,$data=[],$filed = ""){
        list($key,$val,$max) = explode(",",$rule . ',0,0');
  
        if($data[$key] == $val && $value <= $max){
        
            return  ":attribute必须大于".$max; 
        }       
        return true;
    }
    
     /**
     * 验证规格
     * @author 晓风<215628355@qq.com>
     * @param string $value 值
     * @param int $rule 规则
     * @param array $data 数据
     * @param string $filed 字段
     * @return boolean
     */
    protected function checkItems($items,$rule,$data=[],$filed = ""){
        if($data['is_spec'] == 0){
            return true;
        }
        if(!is_array($items)){
            return  ":attribute必须是数组"; 
        }
        $validate = \think\facade\Validate::make($this->itemRole,$this->itemMessage);
        $sort = 0;
        foreach($items as $item){
           $sort ++;
           $res =  $validate->check($item);
           if(!$res){
               return '第'.$sort.'行规格出错：'.$validate->getError();
           }
        }      
        return true;
    }
}
